﻿using System;
using System.Collections.Generic;
using Yxinmt.Interface;
using Yxinmt.Utility;

namespace Yxinmt.Windows.Meeting
{
    public class InkCanvasTab : BaseWindow, IWindow
    {
        Entity.FileInfo _File { get; set; }
        DocumentViewing _DocumentViewing { get; set; }

        public InkCanvasTab(System.Windows.Window form, IWindow parentForm, System.Windows.Controls.Grid pagePanel, Dictionary<string, object> parameters)
        {
            Window = form;
            ParentWindow = parentForm;
            _PagePanel = pagePanel;
            _Parameters = parameters;
            _DocumentViewing = ParentWindow as DocumentViewing;
            _File = parameters.GetValue<Entity.FileInfo>("File");
            InitSet();

            this.SetWindow();

            initLoad();
        }

        static List<InkCanvasTab> _InkCanvasTabList { get; set; }

        static InkCanvasTab()
        {
            _InkCanvasTabList = new List<InkCanvasTab>();
        }

        public static void Dispose()
        {
            _InkCanvasTabList.ForEach(c => c.CloseWindow());
        }

        Controls.InkCanvas _InkCanvas { get; set; }

        InkCanvasProperty _InkCanvasProperty { get; set; }

        void initLoad()
        {
            _InkCanvas = GetControl("InkCanvas1") as Controls.InkCanvas;

            _InkCanvasProperty = new InkCanvasProperty(_InkCanvas);

            _InkCanvasPropertyMenu = new InkCanvasPropertyMenu(this.ParentWindow, _InkCanvasProperty);
        }

        InkCanvasPropertyMenu _InkCanvasPropertyMenu { get; set; }


        public void Save()
        {
            if (_InkCanvasProperty.IsDrawSave())
            {
                var bs = _InkCanvas.SaveInkElements();
                if (bs != null)
                {
                    var imgBytes = _InkCanvas.SaveToPng();
                    TaskRun("SaveFile", true, () =>
                    {
                        string id = Guid.NewGuid().ToString();
                        string urlName = _File.FileName + "_content.png";
                        string dirName = _DocumentViewing.MeetingId.ToString() + "/" + id;
                        var res2 = RequestService.UploadFile(_File.FileName, dirName, urlName, bs);
                        if (!res2.Result)
                        {
                            Alert(res2.Message);
                            return;
                        }

                        var res3 = RequestService.UploadFile(_File.FileName, dirName, _File.FileName, imgBytes);
                        if (!res3.Result)
                        {
                            Alert(res3.Message);
                            return;
                        }

                        Dictionary<string, object> record = new Dictionary<string, object>();
                        record["ContentUrl"] = res2.FileUrl;
                        record["ImageUrl"] = res3.FileUrl;
                        record["FileSize"] = res2.FileSize;
                        record["RecordId"] = _File.FileId;

                        var res = RequestService.SaveRecord(record, true);
                        if (res.Result)
                        {
                            _File.InkCanvasBytes = bs;
                        }
                        else Alert(res.Message);
                    });
                }
            }
        }

        public override void DataLoad()
        {
            base.DataLoad();

            _InkCanvasPropertyMenu.DataLoad();

            if (!string.IsNullOrEmpty(_File.ContentUrl))
            {
                string fileUrl = _File.ContentUrl;
                if (fileUrl.StartsWith("/")) fileUrl = _DocumentViewing.ClientConfig.ServiceUrl + fileUrl;
                var bs = Utils.DownloadFileBytes(fileUrl);
                if (bs != null) _File.InkCanvasBytes = bs;
            }

            if (!string.IsNullOrEmpty(_File.FileUrl))
            {
                string fileUrl = _File.FileUrl;
                if (fileUrl.StartsWith("/")) fileUrl = _DocumentViewing.ClientConfig.ServiceUrl + fileUrl;
                var bs = Utils.DownloadFileBytes(fileUrl);
                if (bs != null) _File.NoteFileImage = bs;
            }

            if (_File.NoteFileImage != null)
            {
                _InkCanvas.DrawBackImage(Utility.Common.GetImageSource(_File.NoteFileImage));
                _InkCanvasPropertyMenu.Color = "red";
            }

            if (_File.InkCanvasBytes != null && _File.InkCanvasBytes.Length > 0) _InkCanvas.SetInkElements(_File.InkCanvasBytes);
           
           
        }

        public override void ShowLoad()
        {
            base.ShowLoad();
        }

        public override void CloseWindow()
        {
            try
            {
                Save();
                base.CloseWindow();
                _InkCanvasTabList.Remove(this);
            }
            catch (Exception ex)
            {
                Utility.LoggerProxy.Exception(Name, "CloseWindow", ex);
            }
        }
    }
}

